<template>
  <div class="select-component" v-if="selections.length > 0">
    <div class="select-selected" @click="toggleDrop">
      <div class="select-input"
           :style="{'width': width, 'height': height, 'line-height': height}">
        {{ selections[nowIndex].name }}
      </div>
      <div :style="{'height': height}" class="select-arrow"></div>
    </div>
    <div class="select-option" v-if="isDrop">
      <ul>
        <li v-for="(item, index) in selections" :key="index" @click="chooseSelection(index)">{{ item.name }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VSelect',
  props: {
    selections: {
      type: Array,
      default () {
        return []
      }
    },
    width: {
      type: String,
      default: '51px'
    },
    height: {
      type: String,
      default: '36px'
    },
    defaultValue: {
      type: [String, Number],
      default: ''
    },
    isDefaultChoose: {
      type: String,
      default: 'true'
    }
  },
  watch: {
    selections (val) {
      this.firstShow(val)
    },
    defaultValue () {
      this.firstShow(this.selections)
    }
  },
  data () {
    return {
      isDrop: false,
      nowIndex: 0
    }
  },
  methods: {
    toggleDrop () {
      this.isDrop = !this.isDrop
    },
    chooseSelection (index) {
      this.nowIndex = index
      this.isDrop = false
      this.$emit('on-change', this.selections[this.nowIndex])
    },
    firstShow (val) {
      if (this.defaultValue) {
        for (let i in val) {
          if (val[i].id === Number(this.defaultValue)) {
            this.nowIndex = i
            return
          }
        }
        if (this.nowIndex === -1) {
          this.nowIndex = 0
        }
      } else {
        this.nowIndex = 0
      }
    }
  },
  mounted () {
    this.firstShow(this.selections)
    document.addEventListener('click', (e) => {
      if (!this.$el.contains(e.target)) {
        this.isDrop = false
      }
    })
  }
}
</script>

<style scoped>
.select-component {
  position: relative;
}

.select-selected {
  overflow: hidden;
  cursor: pointer;
}

.select-input {
  float: left;
  width: 51px;
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  color: #333;
  border: 1px solid #ccc;
  padding-left: 13px;
  border-right: 0;
}

.select-arrow {
  float: left;
  width: 38px;
  height: 36px;
  background-color: #f2f2f2;
  position: relative;
  border: 1px solid #ccc;
}

.select-arrow:after {
  content: '';
  display: block;
  width: 0;
  height: 0;
  border-top: 5px solid #333;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%)
  /*margin-top: -3px;*/
  /*margin-left: -5px;*/
}

.select-option {
  background: #fff;
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #ccc;
  border-top: 0;
  max-height: 200px;
  overflow: auto;
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 4;
}

.select-option ul {
  background-color: #fff;
  position: relative;
}

.select-option li {
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  color: #333;
  padding-left: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.select-option li:hover {
  background-color: #F2F2F2
;
}
</style>
